

logfile="shLog.txt"
pkg_config="pkg-config"

sh_quote(){
    v=$(echo "$1" | sed "s/'/'\\\\''/g")
    test "x$v" = "x${v#*[!A-Za-z0-9_/.+-]}" || v="'$v'"
    echo "$v"
}

for v in "$@"; do
    r=${v#*=}
    l=${v%"$r"}
    r=$(sh_quote "$r")
    FFMPEG_CONFIGURATION="${FFMPEG_CONFIGURATION# } ${l}${r}"
done

log(){
    echo "$@" >> $logfile
}

test_cmd(){
    log "$@"
	echo "test_cmd:$@"
    sudo "$@" >> $logfile 2>&1
}

enabled(){
    test "${1#!}" = "$1" && op="=" || op="!="
    eval test "x\$${1#!}" $op "xyes"
}

disabled(){
    test "${1#!}" = "$1" && op="=" || op="!="
    eval test "x\$${1#!}" $op "xno"
}



log_file(){
    log BEGIN "$1"
    log_file_i=1
    while IFS= read -r log_file_line; do
        printf '%5d\t%s\n' "$log_file_i" "$log_file_line"
        log_file_i=$(($log_file_i+1))
    done < "$1" >> "$logfile"
    log END "$1"
}

cc_o(){
    eval printf '%s\\n' $CC_O
}

ld_o(){
    eval printf '%s\\n' $LD_O
}

set_all(){
    value=$1
    shift
    for var in $*; do
        eval $var=$value
    done
}

enable(){
    set_all yes $*
}


disable(){
    set_all no $*
}

filter(){
    pat=$1
    shift
    for v; do
        eval "case '$v' in $pat) printf '%s ' '$v' ;; esac"
    done
}

filter_out(){
    pat=$1
	echo "pat:$pat"
    shift
	echo "pat shift:$pat"
    for v; do
		echo "v:$v"
        eval "case '$v' in $pat) ;; *) printf '%s ' '$v' ;; esac"
    done
}


die(){
    log "$@"
    echo "$error_color$bold_color$@$reset_color"
    cat <<EOF

If you think configure made a mistake, make sure you are using the latest
version from Git.  If the latest version fails, report the problem to the
ffmpeg-user@ffmpeg.org mailing list or IRC #ffmpeg on irc.libera.chat.
EOF
    if disabled logging; then
        cat <<EOF
Rerun configure with logging enabled (do not use --disable-logging), and
include the log this produces with your report.
EOF
    else
        cat <<EOF
Include the log file "$logfile" produced by configure as this will help
solve the problem.
EOF
    fi
    exit 1
}

TMPDIR=.

FFTMPDIR=$(mktemp -d "${TMPDIR}/ffconf.XXXXXXXX" 2> /dev/null) ||
    die "Unable to create temporary directory in $TMPDIR."

tmpfile(){
    tmp="${FFTMPDIR}/test"$2
    (set -C; exec > $tmp) 2> /dev/null ||
        die "Unable to create temporary file in $FFTMPDIR."
    eval $1=$tmp
}

tmpfile TMPC   .c

test_cc(){
    log test_cc "$@"
    cat > $TMPC
    log_file $TMPC
    test_cmd $cc $CPPFLAGS $CFLAGS "$@" $CC_C $(cc_o $TMPO) $TMPC
}

test_ld(){
    log test_ld "$@"
	echo "test_ld:$@"
    type=$1
    shift 1
	echo "test_ld shift:$@"
    flags=$(filter_out '-l*|*.so' $@)
    libs=$(filter '-l*|*.so' $@)
	echo "type:$type ,flags:$flags ,libs:$libs "
    test_$type $($cflags_filter $flags) || return
    flags=$($ldflags_filter $flags)
    libs=$($ldflags_filter $libs)
	echo "ld:$ld ,LDFLAGS:$LDFLAGS ,LDEXEFLAGS:$LDEXEFLAGS ,TMPO:$TMPO ,extralibs:$extralibs"
    test_cmd $ld $LDFLAGS $LDEXEFLAGS $flags $(ld_o $TMPE) $TMPO $libs $extralibs
}

check_ld(){
    log check_ld "$@"
    type=$1
    name=$2
    shift 2
    disable $name
    test_ld $type $@ && enable $name
}

print_include(){
    hdr=$1
    test "${hdr%.h}" = "${hdr}" &&
        echo "#include $hdr"    ||
        echo "#include <$hdr>"
}

enable_sanitized(){
    for var; do
        enable $(sanitize_var_name $var)
    done
}

sanitize_var_name(){
    echo $@ | sed 's/[^A-Za-z0-9_]/_/g'
}

check_func_headers(){
    log check_func_headers "$@"
    headers=$1
    funcs=$2
	echo "headers:$headers,funcs:$funcs"
    shift 2
    {
        for hdr in $headers; do
            print_include $hdr
        done
        echo "#include <stdint.h>"
        for func in $funcs; do
            echo "long check_$func(void) { return (long) $func; }"
        done
        echo "int main(void) { int ret = 0;"
        # LTO could optimize out the test functions without this
        for func in $funcs; do
            echo " ret |= ((intptr_t)check_$func) & 0xFFFF;"
        done
        echo "return ret; }"
    } | test_ld "cc" "$@" && enable $funcs && enable_sanitized $headers

	echo "====:$@"
}

set_sanitized(){
    var=$1
    shift
    eval $(sanitize_var_name "$var")='$*'
}

test_pkg_config(){
    log test_pkg_config "$@"
    name="$1"
    pkg_version="$2"
    pkg="${2%% *}"
    headers="$3"
    funcs="$4"
    shift 4
    disable $name

	echo $name
	echo $pkg_version
	echo $pkg
	echo $headers
	echo $funcs
	echo $pkg_config

    test_cmd $pkg_config --exists --print-errors $pkg_version || return
    pkg_cflags=$($pkg_config --cflags $pkg_config_flags $pkg)
    pkg_libs=$($pkg_config --libs $pkg_config_flags $pkg)
    check_func_headers "$headers" "$funcs" $pkg_cflags $pkg_libs "$@" &&
        enable $name &&
        set_sanitized "${name}_cflags"    $pkg_cflags &&
        set_sanitized "${name}_extralibs" $pkg_libs
}

append(){
    var=$1
    shift
    eval "$var=\"\$$var $*\""
}

add_cflags(){
    append CFLAGS $($cflags_filter "$@")
}


check_pkg_config(){
    log check_pkg_config "$@"
    name="$1"
    test_pkg_config "$@" &&
        eval add_cflags \$${name}_cflags
}


require_pkg_config(){
    log require_pkg_config "$@"
    pkg_version="$2"
    check_pkg_config "$@" || die "ERROR: $pkg_version not found using pkg-config$pkg_config_fail_message"
}


require_headers(){
    log require_headers "$@"
    headers="$1"
    check_headers "$@" || die "ERROR: $headers not found"
}
# enabled libass
# echo $?
# enable libass

check_lib(){
    log check_lib "$@"
    name="$1"
    headers="$2"
    funcs="$3"
    shift 3
    disable $name
    check_func_headers "$headers" "$funcs" "$@" &&
        enable $name && eval ${name}_extralibs="\$@"
}

# libs=$(filter '-l*|*.so' $@)
# echo "lib:$lib"

# check_lib libass ass/ass.h ass_library_init
# echo $?
# enabled libass
# echo $?




# require_headers /usr/include/ass/ass.h

test_pkg_config libassgl libassgl assgl.h assglGetVersion

# require_pkg_config libass libass ass/ass.h ass_library_init